<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

class JFX_Settings
{
    protected $settings = array();

    public function __construct(){

    }

    public function load(){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        $settings = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}settings");
        $eParams = array('settings' => $settings);
        JFX::hook('JFX_Settings.loadPre', $eParams);
        foreach($settings as $k=>$v){
            $this->settings[$v['k']] = $v['v'];
        }
    }

    public function get($k){
        $v = NULL;
        $eParams = array('k'=>$k, 'v'=>$v);
        JFX::hook('JFX_Settings.getPre', $eParams);
        if(array_key_exists($eParams['k'], $this->settings)) $eParams['v'] = $this->settings[$eParams['k']];
        JFX::hook('JFX_Settings.getPost', $eParams);
        return $eParams['v'];
    }

    public function set($k, $v){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        $eParams = array('k'=>$k, 'v'=>$v);
        JFX::hook('JFX_Settings.setPre', $eParams);
        if(array_key_exists($eParams['k'], $this->settings)){
            $dbParams = array(
                'v' => $eParams['v']
            );
            $this->settings[$eParams['k']] = $eParams['v'];
            $kk = $DB->escape($eParams['k']);
            $DB->update($CONFIG->dbprefix.'settings', $dbParams, "k = '{$kk}'");

        }else{
            $dbParams = array(
                'k' => $eParams['k'],
                'v' => $eParams['v']
            );
            $DB->insert($CONFIG->dbprefix.'settings', $dbParams);
			$this->settings[$eParams['k']] = $eParams['v'];
        }
    }



}